﻿App.Debt = function() {
    return {

        currentFormValues: {},

        getStore: function() {
            var store = new Ext.data.Store({
                autoLoad:true,
				baseParams : {start:0, limit:25},
				proxy: new Ext.data.HttpProxy({
					url : 'cost/debt'
				}),
                remoteSort: false,
				reader: new Ext.data.JsonReader({
					totalProperty : 'totalCount',
					root : 'rows',
					idProperty : 'object_id',
					fields: [{
						name:'cost_id'
					},{
						name: 'object_id',
						type: 'string'
					},
					{
						name :'obj_name',
						type:'string'
					},
					{
						name :'obj_num',
						type:'string'
					},
					{
						name :'objid',
						type:'string'
					},
					{
						name: 'from_name',
						allowBlank: false
					},
					{
						name: 'to_name',
						allowBlank: false
					},
					{
						name: "d_traff_fee"
					},
					{
						name: "d_mid_fee"
					},
					{
						name: "d_inst_fee"
					},
					{
						name: "d_oth_fee"
					},
					{
						name: "d_saf_fee"
					},
					{
						name: "saf_fee"
					},
					{
						name: "t_saf_fee"
					},
					{
						name: "oth_fee"
					},
					{
						name: "t_oth_fee"
					},
					{
						name: "mid_fee"
					},
					{
						name: "t_mid_fee"
					},
					{
						name: "inst_fee"
					},
					{
						name: "t_inst_fee"
					},
					{
						name: "traff_fee"
					},
					{
						name: "t_traff_fee"
					},
					{
						name:"pre_fee"
					},{
						name:"to_tell"
					},{
						name:"cost_name"
					},
					{
						name:"all_debt_fee"
					}]
				})
            });
            return store;
        },



        submit: function() {
            if (this.form.getForm().isValid()) {
                this.form.getForm().submit({
                    waitTitle: '保存数据',
                    waitMsg: '正在保存……',
                    scope: this,
                    method: 'post',
                    params: '',
                    success: function(form, action) {
                       var info = action.result.info;
                        if (action.result.success) {
                            this.store.reload();
                            if (action.result.method == "Create") {
                                this.form.getForm().reset();
                            } else {
                                this.dlg.hide();
                            }
                        }
                        Ext.Msg.alert('信息', info);
                    },
                    failure: function(form, action) {
                        var info = action.result.info;
	    				Ext.Msg.alert('提示', info);
                    }
                });
            }
        },


        createGrid: function(id) {

            var panel = Ext.getCmp(id);

            panel.body.dom.innerHTML = "";

            var sm = new Ext.grid.CheckboxSelectionModel();
            this.grid = new Ext.grid.GridPanel({
            	anchor:'top 50%',
                loadMask: true,
                stripeRows: true,
                tbar: new Ext.PagingToolbar({ 
                	pageSize: 25 ,//分页必须加的参数.最好和store里面的limit保持一致
                	displayInfo: true,
                    store: this.store,
                    items:[
                    /*{
	                    text: '增加',
	                    scope: this,
	                    handler: this.add
	                },*/
	               {
	                    text: '欠款回收',
	                    scope: this,
	                    handler: this.edit
	                },
	                /*{
	                    text: '删除',
	                    scope: this,
	                    handler: this.del
                },*/
                '-', new Ext.ux.form.SearchField({
                    store: this.store,
                    width: 110
                })]}),
                title:"入库管理",//打印必须设置tile
                plugins: [Ext.ux.plugins.Print],//打印按钮
                loadMask: true,
                viewConfig : {autoFill:false,forceFit:true},
                autoHeight:true,
                store: this.store,
                sm: sm,
                columns: [sm,
                          new Ext.grid.RowNumberer(),
                          {
                    header: "货号",
                    width: 80,
                    sortable: true,
                    dataIndex: 'objid',
                    renderer: this.highLight
                },
                {
                    header: "货物名称",
                    width: 120,
                    sortable: true,
                    dataIndex: 'obj_name',
                    renderer: this.highLight
                },
                {
                    header: "件数",
                    width: 60,
                    sortable: true,
                    dataIndex: 'obj_num',
                    renderer: this.highLight
                },
                {
                    header: "收货人",
                    width: 100,
                    sortable: true,
                    dataIndex: 'to_name',
                    renderer: this.highLight
                },
                {
                	header: "收货人电话",
                    width: 100,
                    sortable: true,
                    dataIndex: 'to_tell',
                    renderer: this.highLight
                },
                {
                	header:"付款方式",
                	width:100,
                	dataIndex:'cost_name',
                    renderer: this.highLight
                	
                },
                {
                    header: "运费",
                    width: 100,
                    sortable: true,
                    dataIndex: 'd_traff_fee'
                },
                {
                    header: "保险费",
                    width: 100,
                    sortable: true,
                    dataIndex: 'd_saf_fee',
                    renderer: this.highLight
                },
                {
                    header: "中转费",
                    width: 100,
                    sortable: true,
                    dataIndex: 'd_mid_fee',
                    renderer: this.highLight
                },
                {
                    header: "代收",
                    width: 100,
                    sortable: true,
                    dataIndex: 'd_inst_fee'
                },
                {
                    header: "其它费",
                    width: 100,
                    sortable: true,
                    dataIndex: 'd_oth_fee',
                    renderer: this.highLight
                },
                {
                    header: "垫付费",
                    width: 100,
                    sortable: true,
                    dataIndex: 'pre_fee',
                    renderer: this.highLight
                },
                {
                	header:"总欠费",
                	width:100,
                	dataIndex:'all_debt_fee',
                	renderer: this.highLight
                },
                {
                    header: "时间",
                    width: 150,
                    sortable: true,
                    dataIndex: 'op_time',
                    renderer: Ext.util.Format.dateRenderer('Y-m-d H:i:s')
                }]
            });
            panel.add(this.grid);

        },
        getForm: function() {
        	var locationStore = new Ext.data.Store({
            	autoLoad:false,
//				baseParams:{start:0, limit:23},
				proxy: new Ext.data.HttpProxy({
					url : 'localtion/localtionList'
				}),
                remoteSort: false,
                reader: new Ext.data.JsonReader({
					totalProperty : 'totalCount',
					root : 'rows',
					idProperty : 'local_id',
					fields: [{
						name: 'local_id',
						type: 'string'
					},
					{
						name: 'local_name',
						type: 'string'
					}]
				})
            });
                var getCostTypeStore = new Ext.data.Store({
                    autoLoad:false,
    				baseParams : {parent_id:1},//1表示direct表id为1的记录(次store取出parent为1的所有记录)
    				proxy: new Ext.data.HttpProxy({
    					url : 'menu/costype'
    				}),
                    remoteSort: false,
    				reader: new Ext.data.JsonReader({
    					totalProperty : 'totalCount',
    					root : 'rows',
    					idProperty : 'id',
    					fields: [{
    						name: 'name',
    						type:'string'
    					},{
    						name: 'id',
    						type: 'string'
    					}]
    				})
                });
               
        	var truckStore = new Ext.data.Store({
            	autoLoad:false,
//				baseParams:{start:0, limit:23},
				proxy: new Ext.data.HttpProxy({
					url : 'truck/items'
				}),
                remoteSort: false,
                reader: new Ext.data.JsonReader({
					totalProperty : 'totalCount',
					root : 'rows',
					idProperty : 'truck_id',
					fields: [{
						name: 'truck_id',
						type: 'string'
					},
					{
						name: 'truck_nbr',
						type: 'string'
					}]
				})
            });
        	var staffStore = new Ext.data.Store({
            	autoLoad:false,
//				baseParams:{start:0, limit:23},
				proxy: new Ext.data.HttpProxy({
					url : 'user/users'
				}),
                remoteSort: false,
                reader: new Ext.data.JsonReader({
					totalProperty : 'totalCount',
					root : 'rows',
					idProperty : 'staff_id',
					fields: [{
						name: 'staff_id',
						type: 'string'
					},
					{
						name: 'name',
						type: 'string'
					}]
				})
            });
        	var transitStore = new Ext.data.Store({
            	autoLoad:false,
//				baseParams:{start:0, limit:23},
				proxy: new Ext.data.HttpProxy({
					url : 'transit/items'
				}),
                remoteSort: false,
                reader: new Ext.data.JsonReader({
					totalProperty : 'totalCount',
					root : 'rows',
					idProperty : 'transit_id',
					fields: [{
						name: 'transit_id',
						type: 'string'
					},
					{
						name: 'name',
						type: 'string'
					}]
				})
            });
            var frm = new Ext.form.FormPanel({
            	url:'cost/manager',
                frame:true,
                labelAlign:'right',
                waitMsgTarget:true,
                
                items:[
                   {
                	layout:'column',
//                	width:400,
                	items:[
                	{
                		layout:'form',
//                		columnWidth:.80,
                		defaultType: 'textfield',
                		readOnly:true,
                		items:[
                		{
                			name: 'objid',
                			fieldLabel: '货号'
                		},
                		{
                			name: 'traff_fee',
                			fieldLabel: '运费',
                			xtype : 'numberfield',
                            value:0,
                            minValue:0
                		},
                		{
                			name: 'mid_fee',
                			fieldLabel: '中转费',
                			xtype : 'numberfield',
                            value:0,
                            minValue:0
                		},
                		{
                			name: 'saf_fee',
                			fieldLabel: '保险费',
                			xtype : 'numberfield',
                            value:0,
                            minValue:0
                		},
                		{
                			name: 'inst_fee',
                			fieldLabel: '代收费',
                			xtype : 'numberfield',
                            value:0,
                            minValue:0
                		},
                		{
                			name: 'oth_fee',
                			fieldLabel: '其它费',
                			xtype : 'numberfield',
                            value:0,
                            minValue:0
                		},
                		{
                			name:'pre_fee',
                			fieldLabel:'垫付费',
                			xtype : 'numberfield',
                            value:0,
                            minValue:0
                		}]
                	},
                	{
                		layout:'form',
//					     columnWidth:.50,
					     items:[
		        		{
		            		xtype: 'combo',
		            		fieldLabel:"付款方式",
		        			name: 'cost_type',
		        			width:130,
		        			store :getCostTypeStore,
		        			 displayField:'name',
		                     valueField :'id',
		                     mode: 'remote',
		                     triggerAction: 'all',
		                     editable: false,
		                     emptyText:'选择付款方式....'
		        		},
                		{
                			xtype: 'textfield',
                			name: 't_traff_fee',
                			fieldLabel: '已收',
                			xtype : 'numberfield',
                            value:0,
                            minValue:0
                		},
                		{
                			xtype: 'textfield',
                			name: 't_mid_fee',
                			fieldLabel: '已收',
                			xtype : 'numberfield',
                            value:0,
                            minValue:0
                		},
                		{
                			xtype: 'textfield',
                			name: 't_saf_fee',
                			fieldLabel: '已收',
                			xtype : 'numberfield',
                            value:0,
                            minValue:0
                		},
                		{
                			xtype: 'textfield',
                			name: 't_inst_fee',
                			fieldLabel: '已收',
                			xtype : 'numberfield',
                            value:0,
                            minValue:0
                		},
                		{
                			xtype: 'textfield',
                			name: 't_oth_fee',
                			fieldLabel: '已收',
                			xtype : 'numberfield',
                            value:0,
                            minValue:0
                		},
                		{
                			xtype: 'textfield',
                			name: 't_all_fee',
                			fieldLabel: '已收',
                			xtype : 'numberfield',
                            value:0,
                            minValue:0
                		}]
                	}]
	                          
                },
                {
                	xtype: 'hidden',
                	name:'cost_id'
                	
                },
                {
                	xtype: 'hidden',
                	name:'object_id'
                	
                },
                {
            	xtype: 'radiogroup', 
                fieldLabel: '是否结清',
                items : [
                         {
							boxLabel: '是',
							name: 'settle',
							inputValue:1,
							checked : true
                         },
                         {
                             boxLabel: '否',
                             name: 'settle',
                             inputValue:0
                         }
                      ] 
            }], buttons: [{
                    text: '确定',
                    scope: this,
                    handler: function() {
                        this.submit();
                    }
                },
                {
                    text: '重置',
                    scope: this,
                    handler: function() {
                        var f = this.form.getForm();
                        f.reset();
                        f.setValues(this.currentFormValues);
                        f.clearInvalid();
                    }
                }]
            }); //FormPanel
            return frm;
        },
        getDialog: function() {
            var dlg = new Ext.Window({
                width: 550,
                height: 300,
                title: '',
                plain: true,
                closable: true,
                resizable: false,
                frame: true,
                layout: 'fit',
                closeAction: 'hide',
                border: false,
                modal: true,
                items: [this.form],
                listeners: {
                    scope: this,
                    render: function(fp) {
                        this.form.getForm().waitMsgTarget = fp.getEl();
                    },
                    show: function() {
                        var f = this.form.getForm();
                 //       f.findField("roleList").store.reload();
                        f.setValues(this.currentFormValues);
                        f.clearInvalid();
//                        if (this.currentFormValues.id) {
//                            Ext.get("pwdMsg").dom.innerHTML = "<span style='color:red;line-height:30px;'>*如果不修改密码请不要输入密码和确认密码</span>";
//                            f.findField("password").allowBlank = true;
//                            f.findField("confirmPassword").allowBlank = true;
//                        } else {
//                            f.findField("password").allowBlank = false;
//                            f.findField("confirmPassword").allowBlank = false;
//                            Ext.get("pwdMsg").dom.innerHTML = "<span style='color:red;'></span>";
//                        }
                    }
                }
            });
            return dlg;
        },
        highLight: function(value) {
        	var key = App.Debt.grid.store.baseParams.query;
        	var v = value ? value.toString() : '';
        	if (Ext.isString(v) & Ext.isString(key)) {
        		if (key.length > 0) {
        			var re = new RegExp(key, "g");
        			return v.replace(re, "<span style='color:red;background-color:yellow;'>" + key + "</span>");
        		} else {
        			return v;
        		}
        	} else {
        		return v;
        	}
        },
        add: function() {
            Ext.apply(this.currentFormValues, {
                id: '',
                name: "",
                password: "",
                confirmPassword: "",
                roleList: ""
            });
            this.dlg.setTitle("增加货号");
            this.dlg.show();
        },

        edit: function() {
            if (this.grid.getSelectionModel().hasSelection()) {
                this.dlg.setTitle("编辑欠款");
                var selectdata = this.grid.getSelectionModel().getSelected().data;;
                Ext.apply(this.currentFormValues, {
            		cost_id:selectdata.cost_id,
            		objid:selectdata.objid,
            		saf_fee:selectdata.saf_fee,
            		mid_fee:selectdata.mid_fee,
            		traff_fee:selectdata.traff_fee,
            		inst_fee:selectdata.inst_fee,
            		oth_fee:selectdata.oth_fee,
            		t_saf_fee:selectdata.saf_fee,
            		t_mid_fee:selectdata.mid_fee,
            		t_traff_fee:selectdata.traff_fee,
            		t_inst_fee:selectdata.inst_fee,
            		t_oth_fee:selectdata.oth_fee,
            		cost_id:selectdata.cost_id
            		
               });
                this.dlg.show();
            } else {
                Ext.Msg.alert('信息', '请选择要回收的欠款。');
            }
        },

        del: function() {
            if (this.grid.getSelectionModel().hasSelection()) {
                var recs = this.grid.getSelectionModel().getSelections();
                var ids = [];
                var titles = '';
                for (var i = 0; i < recs.length; i++) {
                    var data = recs[i].data;
                    ids.push(data.object_id);
                    titles += data.objid + '<br>';
                }
                Ext.Msg.confirm('删除用户', '确定删除货号？<br><font color="red">' + titles + '</font>',
                function(btn) {
                    if (btn == 'yes') {
						Ext.Ajax.request({
							method:'post',
							url:'inhouse/remove',
							params:{
								ids:ids.toString()
							},
							success: function(resp, opts) {
								var result = Ext.util.JSON.decode(resp.responseText);
								var info = result.info;
								if(result.success=='true') {
									Ext.Msg.alert('信息', info);
                                    App.Debt.store.reload();
								}else {
                                    Ext.Msg.alert('信息', info);
                                }
							},
							failure: function(resp,opts) {
								var result = Ext.util.JSON.decode(resp.responseText);
								var info = result.info;
								Ext.Msg.alert('提示', info); 
							}
						});
                    }
                });
            } else {
                Ext.Msg.alert('信息', '请选择要删除的用户！');
            }
        },


        render: function(id) {
            if (!this.store) {
                this.store = this.getStore();
            };
            if (!this.form) {
                this.form = this.getForm();
            };
            if (!this.dlg) {
                this.dlg = this.getDialog();
            };
            this.createGrid(id);

        }
    };
} ();
